package io.gitee.twoke.generator.engine.impl.generate.impl;

import io.gitee.twoke.generator.engine.impl.generate.ImportTableBuilder;
import io.gitee.twoke.generator.engine.impl.metadata.Table;
import io.gitee.twoke.generator.engine.impl.metadata.TableColumn;
import io.gitee.twoke.generator.engine.impl.metadata.impl.TableImpl;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

public class ImportTableBuilderImpl implements ImportTableBuilder {

    private GenerateServiceImpl generateService;
    private List<TableColumn> columns;
    private TableImpl table;
    private boolean isLoadDatabase = true;

    public ImportTableBuilderImpl(GenerateServiceImpl generateService) {
        this.generateService = generateService;
        this.table = TableImpl.builder().build();
    }

    public boolean isLoadDatabase() {
        return isLoadDatabase;
    }

    public Table getTable() {
        return table;
    }

    public List<TableColumn> getColumns() {
        return columns;
    }

    @Override
    public void importTable() {
        this.generateService.importTable(this);
    }

    @Override
    public ImportTableBuilder tableName(String name) {
        this.table.setName(name);
        return this;
    }

    @Override
    public ImportTableBuilder tableComment(String comment) {
        this.table.setComment(comment);
        return this;
    }


    @Override
    public ImportTableBuilder column(TableColumn column) {
        if (Objects.isNull(columns)) {
            this.columns = new ArrayList<>();
        }
        if (Objects.nonNull(column) && !columns.contains(column)) {
            this.columns.add(column);
            this.isLoadDatabase = false;
        }
        return this;
    }

    @Override
    public ImportTableBuilder columns(List<TableColumn> columns) {
        if (Objects.nonNull(columns)) {
            this.columns = columns;
            this.isLoadDatabase = false;
        }
        return this;
    }
}
